package com.fg.estate.controller;

import com.fg.estate.bean.Admin;
import com.fg.estate.bean.Role;
import com.fg.estate.service.AdminService;
import com.fg.estate.service.RoleService;
import com.fg.estate.util.FileUitls;
import com.fg.estate.util.SecurityUils;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author 丰哥
 * 2022/10/13
 *
 * 管理员控制器
 */

@Controller
public class AdminController {

    @Autowired
    private RoleService roleService;

    @Autowired
    private AdminService adminService;


    // 管理员列表画面
    @RequestMapping("/admin/listUi")
    public String listUi(ModelMap map, Integer pageNum) {

        if (pageNum == null) {
            pageNum = 1;
        }

        try {
            // 假设第一个启动页面， 当前页数1，每页显示的数量3
            // 通过业务层获得分页信息
            PageInfo<Map<String, Object>> pageInfo
                    = adminService.getPageInfo(pageNum, 3);  // 参数1=当前页，参数2=每页显示数量

            map.addAttribute("pageInfo", pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return "admin_list";
    }

    // 添加画面
    @RequestMapping("/admin/addUi")
    public String addUi(ModelMap map) {
        try {
            List<Role> roles = roleService.selectAll();
            map.addAttribute("roles", roles);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "admin_add";
    }

    // 添加
    @RequestMapping("/admin/add")
    public String add(Admin admin, MultipartFile headerFile) {

        System.out.println("admin = " + admin);

        String fileName = "";
        try {
            fileName = FileUitls.fileUpload(headerFile);
        } catch (IOException e) {
            e.printStackTrace();
        }

        // 设置数据
        admin.setHeader(fileName);
        admin.setCreateTime(new Date());

        // 加密
        // 设置随机盐
        String salt = SecurityUils.getSalt();
        admin.setSalt(salt);
        // 设置盐加密后的密码
        String password = SecurityUils.getMD5Password(admin.getPassword(), salt);
        // 重新设置加密后的密码
        admin.setPassword(password);

        try {
            adminService.insert(admin);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return "redirect:/admin/addUi";
    }


    // 登录画面
    @RequestMapping("/loginUi")
    public String loginUi() {
        return "login";
    }

    // 登录
    @RequestMapping("/login")
    public String login(String username, String password, HttpSession session) {
        try {
            Admin admin = adminService.login(username, password);
            if(admin != null) {
                // 登录成功
                session.setAttribute("admin", admin);
                return "redirect:/indexUi";
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        // 登录失败
        return "redirect:/loginUi";
    }

    // 退出登录
    @RequestMapping("/logout")
    public String logout(HttpSession session) {
        session.removeAttribute("admin");
        return "redirect:/loginUi";
    }

}
